﻿/******************************************************************************
* File description: DefSet.qml
* Function: 默认设置页面
* Current Version: 1.1
* Author: 陈乃瑞
* Date: 2025-07-01
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.0
import "../components"
import QtCharts 2.3
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.2
import QtQuick.Controls 2.12
import QtMultimedia 5.14

Rectangle {
    width: parent.width
    height: parent.height
    radius: 12
    property int btflag: 1
    property int beginx: 65*dp
    property var alldata: CExeConfig.getdeff(btflag)
    property int tabvis: 0
    property bool zsqvis: true
    property int pbflag: 3
    property int tab4: 3
    property int beginx4: 65*dp
    property var pbtab: CExeConfig.getMaskingInformation()
    property var mkpz: CMaintenance.getComponentInfoList(tab4)
    function clearCanvas() {
        var ctx = pinid.getContext('2d');
        ctx.clearRect(0,0,pinid.width,pinid.height);
        pinid.requestPaint();
        var ctx4 = pinid4.getContext('2d');
        ctx4.clearRect(0,0,pinid4.width,pinid4.height);
        pinid4.requestPaint();
    }
    onVisibleChanged: {
        CExeConfig.delnulldeff()
        alldata=CExeConfig.getdeff(btflag)
        password.setText(CExeConfig.passwordRead())
        pbtab=CExeConfig.getMaskingInformation()
        var value = CExeConfig.getMaskingInformation()
        if(value[0]==="1"&&value[1]==="0"){
            pbflag=1
            tabvis=1
            titleModel.index=0
        }else if(value[0]==="0"&&value[1]==="1"){
            pbflag=2
        }else{
            pbflag=3
        }
    }
    Connections{  // qmlre
        target: CExeConfig
        onMaskingInformation:{
            if(value[0]==="1"&&value[1]==="0"){
                pbflag=1
                tabvis=3
                titleModel.index=titleModel.model.length-1
            }else if(value[0]==="0"&&value[1]==="1"){
                pbflag=2
                tabvis=3
                titleModel.index=titleModel.model.length-1
            }else{
                pbflag=3
                tabvis=3
                titleModel.index=titleModel.model.length-1
            }
        }
    }
    Toast{
        id: toast3
        y: 100*dp
        x: parent.width / 2 - 150*dp
        z: 1
    }
    Connections{
        target: CExeConfig
        onDefrename:{
            toast3.show("名称不可重复！",1)
        }
    }
    CustomPopupBoxWarning{
        id: defdef
        showRejectedButton: false
        box_text: "请将其他类型设置为默认状态！"
    }
    CustomPopupBoxWarning{
        id: defdata
        showRejectedButton: false
        box_text: "默认数据不可删除！"
    }
    Rectangle{
        x:0*dp
        width: parent.width
        y:50*dp
        height: 1*dp
        color: "#E6E6E6"
    }
    CustomPopupBoxWarning{
        id: gzadd
        box_text: "故障信息不允许删除，请确认是否添加！"
        onAccepted: {
            CMaintenance.addFaultInformation()
            gzdata=CMaintenance.getFaultInformation()
        }
    }
    CustomPopupBoxWarning{
        id: passworddef
        showRejectedButton: true
        box_text: "是否确认修改打靶冷却密码？"
        onAccepted: {
            CExeConfig.passwordWrite(password.getText())
            toast3.show("修改成功",0)
        }
    }
    CustomPopupBoxWarning{
        id: pbfun
        showRejectedButton:false
        box_text: "两个模块不可同时屏蔽！"
    }
    CustomTabButton{
        id:titleModel
        model: pbflag===1?["国际化", "修改密码", "模块配置"]:["纯化设置", "国际化", "修改密码", "模块配置"]
        buttonW: 120*dp
        defaultMode1: true
        x:pbflag===1?parent.width/2-180*dp:parent.width/2-240*dp
        y:10*dp
        height: 40*dp
        button_color_press: "#D6E7FF"
        button_color_hover: "#D6E7FF"
        button_color_select: "#D6E7FF"
        onTextChanged: {
            CExeConfig.delnulldeff()
            if(text==="纯化设置"){
                tabvis=0
            }else if(text==="国际化"){
                tabvis=1
            }else if(text==="修改密码"){
                tabvis=2
            }else if(text==="模块配置"){
                tabvis=3
            }
        }
    }
    Rectangle{
        id: layout_list
        x:parent.width*0.15
        y:80*dp
        width: parent.width*0.7
        height: parent.height*0.7
        visible: tabvis===0
        onWidthChanged: {
            pinid.requestPaint()
        }
        RowLayout{
            spacing: 10*dp
            CustomButton{
                id:bt1
                width: 150*dp
                height: 46*dp
                title: "注射器类型"
                button_border_select: "#1677FF"
                button_color_select: "#ecf5ff"
                text_color_select: "#1677FF"
                isSelected: btflag===1
                defaultMode2:true
                onReleased: {
                    CExeConfig.delnulldeff()
                    zsqvis=true
                    btflag=1
                    beginx=65*dp
                    clearCanvas()
                    alldata= CExeConfig.getdeff(btflag)
                }
            }PushButton{
                id:bt2
                width: 150*dp
                height: 46*dp
                title: "盖类型"
                button_border_select: "#1677FF"
                button_color_select: "#ecf5ff"
                text_color_select: "#1677FF"
                isSelected: btflag===2
                defaultMode2:true
                onReleased: {
                    CExeConfig.delnulldeff()
                    zsqvis=false
                    btflag=2
                    beginx=225*dp
                    clearCanvas()
                    alldata= CExeConfig.getdeff(btflag)
                }
            }CustomButton{
                id:bt3
                width: 150*dp
                height: 46*dp
                title: "耗材名称"
                button_border_select: "#1677FF"
                button_color_select: "#ecf5ff"
                text_color_select: "#1677FF"
                isSelected: btflag===3
                defaultMode2:true
                onReleased: {
                    CExeConfig.delnulldeff()
                    zsqvis=true
                    btflag=3
                    beginx=385*dp
                    clearCanvas()
                    alldata= CExeConfig.getdeff(btflag)
                }
            }
        }
        Canvas{
            id:pinid
            width: parent.width
            height: 75*dp
            x:0*dp
            y:30*dp
            onPaint: {
                var ctx = getContext("2d");
                draw(ctx);
            }
            function draw(ctx ) {
                // 画之前清空画布
                ctx.clearRect(0, 0, parent.width, parent.height);
                ctx.fillStyle ="#111";           // 设置画笔属性
                ctx.lineWidth = 1.2*dp
                ctx.strokeStyle = "#B8B7B5"
                ctx.beginPath();
                var px = 0;
                var py = 0;
                ctx.beginPath()
                ctx.moveTo(0, 45*dp)
                ctx.lineTo(beginx, 45*dp)
                ctx.stroke()
                ctx.moveTo(beginx, 45*dp)
                ctx.lineTo(beginx+10*dp, 35*dp)
                ctx.stroke()
                ctx.moveTo(beginx+10*dp, 35*dp)
                ctx.lineTo(beginx+20*dp, 45*dp)
                ctx.stroke()
                ctx.moveTo(beginx+20*dp, 45*dp)
                ctx.lineTo(layout_list.width, 45*dp)
                ctx.stroke()
            }
        }
        Text {
            x:20*dp
            y:90*dp
            text: qsTr("名称")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
        Text {
            x:(layout_list.width-238*dp+width+50*dp)/2
            y:90*dp
            visible: zsqvis
            text: {
                if(btflag===1){
                    return qsTr("       内径(mm)")
                }else{
                    return qsTr("注射器数量(支)")
                }
            }
            color: "#666666"
            font.pixelSize: config.fontsize
        }
        Text {
            x:layout_list.width - 230*dp; y:90*dp
            text: qsTr("是否默认")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
        Text {
            x:layout_list.width - 95*dp;  y:90*dp
            text: qsTr("操作")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
        Rectangle{
            x:0*dp
            y:118*dp
            height: 2*dp
            width: defmod.width
            color: "#DEDEDE"
        }
        ScrollView {
            x:0*dp
            y:120*dp
            width: parent.width
            height: parent.height-40*dp
            clip: true
            Column{
                id: myColumnLayout
                width: parent.width
                height: parent.height
                spacing: 1*dp
                Repeater{
                    id:defmod
                    model: alldata
                    delegate: Item {
                        x:0*dp
                        width: layout_list.width
                        height: 60*dp
                        DefTabCOM{
                            width: parent.width
                            height: parent.height
                            defname: modelData.name
                            isdef: modelData.isdef==="1"
                            number: modelData.num
                            defnamev: modelData.namev
                            visb: zsqvis
                            typeid:btflag
                            onBt2: {
                                CExeConfig.adddeff(btflag)
                                alldata=CExeConfig.getdeff(btflag)
                            }
                            onBt3: {
                                if(isdef){
                                    defdata.show()
                                }else{
                                    if(CExeConfig.deldeff(btflag,number)){

                                    }else{
                                        toast3.show("已经在维护校正中使用不允许删除",1)
                                    }
                                    alldata=CExeConfig.getdeff(btflag)
                                }
                            }
                            onBt1: {
                                if(isdef){
                                    defdef.show()
                                    isdef=true
                                    alldata=CExeConfig.getdeff(btflag)
                                }else{
                                    CExeConfig.defset(typeid,number)
                                    alldata=CExeConfig.getdeff(btflag)
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    Rectangle{
        x:parent.width*0.1
        y:80*dp
        width: parent.width*0.8
        height: parent.height*0.7*dp
        visible: tabvis===1
        color: "white"
        CustomComboBox{
            x:parent.width/2-160*dp
            width: 320*dp
            height: 40*dp
            y:20*dp
            model: ["中文","English"]
            editDefaultText: "中文"
            title: "国际化"
            titleWidth: 60*dp
            editWidth: 260*dp
            onClickText: {
                if(text==="English"){
                    qmlLanguage.setLanguage(0);
                }else{
                    qmlLanguage.setLanguage(1);
                }
            }
        }
    }
    Rectangle{
        x:parent.width*0.1
        y:80*dp
        width: parent.width*0.8
        height: parent.height*0.7*dp
        visible: tabvis===2
        color: "white"
        Item{
            y: 100*dp
            anchors.horizontalCenter: parent.horizontalCenter
            width: 440*dp; height: parent.height-140*dp
            Column{
                anchors.fill: parent
                spacing: 20*dp
                CustomInputText{
                    id: password
                    required: true
                    title: "打靶冷却密码"
                    editWidth: 320*dp
                    titleWidth: 120*dp
                    defpass: true
                }
                Text {
                    height: 30*dp
                    text: qsTr("<font color='red'>*</font><font color='#999999'>注：密码为4-6位数字</font>")
                }
                Rectangle { height: 120*dp; width: 20*dp }
                PushButton{
                    title: "确认"
                    anchors.right: parent.right
                    defaultMode1: true
                    onReleased: {
                        // 使用正则表达式校验
                        const isValid = /^\d{4,6}$/.test(password.getText());
                        if (isValid) {
                            passworddef.show()
                        } else {
                            toast3.show("请输入4-6位数字",1)
                        }
                    }
                }
            }
        }
    }
    Rectangle{
        id:tab444
        x:parent.width*0.15
        y:80*dp
        width: parent.width*0.7
        height: parent.height*0.8
        visible: tabvis===3
        color: "white"
        RowLayout{
            spacing: 10*dp
            CustomButton{
                id:bt41
                width: 150*dp
                height: 46*dp
                title: "转靶"
                button_border_select: "#1677FF"
                button_color_select: "#ecf5ff"
                text_color_select: "#1677FF"
                isSelected: tab4===3
                defaultMode2:true
                onReleased: {
                    tab4=3
                    beginx4=65*dp
                    clearCanvas()
                    pbcl.checked=pbtab[0]==="1"
                    mkpz= CMaintenance.getComponentInfoList(tab4)
                }
            }CustomButton{
                id:bt42
                width: 150*dp
                height: 46*dp
                title: "纯化"
                button_border_select: "#1677FF"
                button_color_select: "#ecf5ff"
                text_color_select: "#1677FF"
                isSelected: tab4===4
                defaultMode2:true
                onReleased: {
                    tab4=4
                    beginx4=225*dp
                    clearCanvas()
                    pbcl.checked=pbtab[1]==="1"
                    mkpz= CMaintenance.getComponentInfoList(tab4)
                }
            }
        }
        Canvas{
            id:pinid4
            width: parent.width
            height: 75*dp
            x:0*dp
            y:30*dp
            onPaint: {
                var ctx = getContext("2d");
                draw(ctx);
            }
            function draw(ctx ) {
                // 画之前清空画布
                ctx.clearRect(0, 0, parent.width, parent.height);
                ctx.fillStyle ="#111";           // 设置画笔属性
                ctx.lineWidth = 1.2*dp
                ctx.strokeStyle = "#B8B7B5"
                ctx.beginPath();
                var px = 0;
                var py = 0;
                ctx.beginPath()
                ctx.moveTo(0, 45*dp)
                ctx.lineTo(beginx4, 45*dp)
                ctx.stroke()
                ctx.moveTo(beginx4, 45*dp)
                ctx.lineTo(beginx4+10*dp, 35*dp)
                ctx.stroke()
                ctx.moveTo(beginx4+10*dp, 35*dp)
                ctx.lineTo(beginx4+20*dp, 45*dp)
                ctx.stroke()
                ctx.moveTo(beginx4+20*dp, 45*dp)
                ctx.lineTo(layout_list.width, 45*dp)
                ctx.stroke()
            }
        }
        Text {
            x:0*dp
            y:90*dp
            width: parent.width*0.2
            text: qsTr("【放靶】【打靶】【转靶】模块为联动关系，将全部启用或全部关闭")
            color: "#666666"
            font.pixelSize: config.fontsize-2
        }
        CustomSwitchButton{
            x:parent.width-120*dp
            y:88*dp
            height: 24*dp; width: 46*dp
            checked: pbtab[0]==="1"
            id:pbcl
            onClicked: {
                if(tab4===3){
                    if(checked&&pbtab[1]==="0"){
                        pbfun.show()
                    }else{
                        checked=!checked
                        pbtab[0]=checked?"1":"0"
                        console.log(pbtab)
                    }
                }else if(tab4===4){
                    if(checked&&pbtab[0]==="0"){
                        pbfun.show()
                    }else{
                        checked=!checked
                        pbtab[1]=checked?"1":"0"
                        console.log(pbtab)
                    }
                }
            }
        }
        Text {
            x:parent.width-70*dp
            y:90*dp
            width: 70*dp
            text: qsTr("模块启用")
//            color: "#666666"
            font.pixelSize: config.fontsize-1
        }
        Rectangle{
            x:0*dp
            y:120*dp
            height: 2*dp
            width: parent.width
            color: "#DEDEDE"
        }
        Text {
            x:0*dp
            y:130*dp
            width: parent.width*0.2
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            text: qsTr("序号")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
        Text {
            x:parent.width*0.2
            y:130*dp
            width: parent.width*0.6
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            text: "实验组件"
            color: "#666666"
            font.pixelSize: config.fontsize
        }
        Text {
            x:parent.width*0.8
            y:130*dp
            width: parent.width*0.2
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            text: qsTr("是否启用")
            color: "#666666"
            font.pixelSize: config.fontsize
        }
        Rectangle{
            x:0*dp
            y:160*dp
            height: 2*dp
            width: parent.width
            color: "#DEDEDE"
        }
        ScrollView {
            x:0*dp
            y:163*dp
            width: parent.width
            height: parent.height-170*dp
            clip: true
            Column{
                id: myColumnLayout4
                width: tab444.width
                spacing: 1*dp
                Repeater{
                    id:defmod4
                    model: mkpz
                    delegate: Item {
                        x:0*dp
                        width: tab444.width
                        height: 60*dp
                        DefTab4COM{
                            id:def4
                            width: parent.width
                            height: parent.height
                            xh:mkpz[index][0]
                            syzj:mkpz[index][1]
                            ispbv:mkpz[index][2]==="1"
                            type:tab4
                            onRefre: {
                                if(tab4===3){
                                    if(ispbv){
                                        mkpz[index][2]="1"
                                    }else{
                                        mkpz[index][2]="0"
                                    }
                                }else if(tab4===4){
                                    if(xh==="6"||xh==="7"){
                                        if(ispbv){
                                            mkpz[5][2]="1"
                                            mkpz[6][2]="1"
                                            mkpz[7][2]="0"
                                        }else{
                                            mkpz[5][2]="0"
                                            mkpz[6][2]="0"
                                            mkpz[7][2]="1"
                                        }
                                    }else if(xh==="8"){
                                        if(ispbv){
                                            mkpz[5][2]="0"
                                            mkpz[6][2]="0"
                                            mkpz[7][2]="1"
                                        }else{
                                            mkpz[5][2]="1"
                                            mkpz[6][2]="1"
                                            mkpz[7][2]="0"
                                        }
                                    }else{
                                        if(ispbv){
                                            mkpz[index][2]="1"
                                        }else{
                                            mkpz[index][2]="0"
                                        }
                                    }
                                    defmod4.model=mkpz
                                }
                            }
                        }
                    }
                }
            }
        }

        PushButton{
            x:parent.width-120*dp
            y:parent.height
            title: "确认"
            anchors.right: parent.right
            defaultMode1: true
            onReleased: {
                CExeConfig.updataMaskingInformation(pbtab)
                CMaintenance.saveComponentInfo(tab4,mkpz)
                toast3.show("修改成功",0)
            }
        }
    }
}
